const app = getApp();
const common = require('../../utils/common.js');
const api = require('../../config/api.js');

var pageNo = 1;
var hadLastPage = false;
var isLoad = 0;

Page({
  data: {
    imgUrl: app.globalData.imgurl,
    leixingShow: false,
    blankBoxShow: false,
    scrollTop: 0,
    scrollHeight: 0,
    imageurl1: "/static/images/desc1.png",
    imageurl2: "/static/images/desc.png",
    imageurl3: "/static/images/desc1.png",
    soldOut: 'asc',
    price: 'asc',
    selectAll: 0,
    selectBySoldOut: 0,
    selectByPrice: 0,
    selectByNew: 0,
    tagId: '',
    keyWord: '',
    productCategoryList: [],
    productCategoryId: '',
    sort:'',
    dataList: [],
    showNoData: false,
    showLoding: true,
    keyword: '',
    parentCategoryId: '',
  },
  onLoad: function (options) {
    var keyword = '';
    if (options.keyword != null && options.keyword != 'undefined' && options.keyword != '') {
      keyword = options.keyword;
    }
    var parentCategoryId = '';
    if (options.parentCategoryId != null && options.parentCategoryId != 'undefined' && options.parentCategoryId != '') {
      parentCategoryId = options.parentCategoryId
    }
    var categoryId = '';
    if (options.categoryId != null && options.categoryId != 'undefined' && options.categoryId != '') {
      categoryId = options.categoryId
    }
    var tagId = '';
    if (options.tagId != null && options.tagId != 'undefined' && options.tagId != '') {
      tagId = options.tagId
    }
    this.setData({
      keyword: keyword,
      parentCategoryId: parentCategoryId,
      productCategoryId: categoryId,
      tagId: tagId
    });
  },
  onShow: function () {
    this.setData({
      sort: ''
    }); 
    this.getCategoryList();
    this.clearData();
    this.loadList();
  },
  getCategoryList: function () {
    let that = this;
    var data = {
      parentCategoryId: that.data.parentCategoryId,
      productCategoryId: that.data.productCategoryId
    }
    common.request(api.ProductCategoryList, data, 'post').then(function (data) {
      if (data.type === 'success') {
        that.setData({
          productCategoryList: data.data.productCategoryList
        });
      }
    });
  },
  clearData: function () {
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      dataList: []
    });
  },
  loadList: function () {
    isLoad = 1;
    if (hadLastPage) {
      isLoad = 0;
      return;
    }
    var that = this;
    var data = {
      pageNo: pageNo,
      keyword: that.data.keyword,
      parentCategoryId: that.data.parentCategoryId,
      productCategoryId: that.data.productCategoryId,
      sort: that.data.sort,
      tagId: that.data.tagId
    }
    this.setData({
      showLoding: true,
      showNoData: false
    });
    setTimeout(function () {
      common.request(api.ProductListData, data, 'POST').then(function (data) {
        that.setData({
          showLoding: false
        });
        if (data.type == 'success') {
          var dataList = that.data.dataList;
          for (var i = 0; i < data.data.dataList.length; i++) {
            dataList.push(data.data.dataList[i]);
          }
          if (data.data.pageNo == data.data.totalPages) {
            hadLastPage = true;
          } else {
            pageNo++;
          }
          that.setData({
            dataList: dataList
          });
          isLoad = 0;
          if (data.data.total == 0) {
            that.setData({
              showNoData: true
            });
          }
        } else {
          console.log(data.content);
          isLoad = 0;
        }
      });
    }, app.globalData.showLodingTime);
  },
  onReachBottom: function () {
    this.loadList();
  },
  keywordInput: function (e) {
    var that = this;
    that.setData({
      keyword: e.detail.value
    });
  },
  onKeywordConfirm: function () {
    this.clearData();
    this.loadList();
  },
  //类型选择
  typeSelect: function (event) {
    var that = this;
    var id = event.currentTarget.dataset.id;
    var sclist = this.data.productCategoryList;
    for (var i = 0; i < sclist.length; i++) {
      var item = sclist[i];
      item.selected = "";
      if (item.id == id) {
        item.selected = "selected";
      }
    }
    this.setData({
      leixingShow: false,
      blankBoxShow: false,
    });
    that.setData({
      productCategoryList: sclist,
      productCategoryId: id,
    });
    this.clearData();
    this.loadList();
  },
  //类型
  zonghe: function (e) {
    var leixingShow = !this.data.leixingShow;
    var blankBoxShow = !this.data.blankBoxShow;
    this.setData({
      leixingShow: leixingShow,
      blankBoxShow: blankBoxShow,
    })
  },
  //点击黑色遮罩
  blankBoxClick: function (e) {
    this.setData({
      leixingShow: false,
      blankBoxShow: false
    })
  },
  //销量
  choosesort1: function (e) {
    this.setData({
      imageurl3: "/static/images/desc1-s.png",
      imageurl1: "/static/images/desc1.png",
      imageurl2: "/static/images/desc.png",
      soldOut: 'asc',
      selectAll: 0,
      selectBySoldOut: 1,
      selectByNew: 0,
      selectByPrice: 0,
      leixingShow: false,
      blankBoxShow: false,
      sort: 'salesDesc'
    })
    this.clearData();
    this.loadList();
  },
  //价格
  choosesort2: function (e) {
    if (this.data.price == 'desc') {
      this.setData({
        imageurl2: "/static/images/desc-up.png",
        imageurl3: "/static/images/desc1.png",
        imageurl1: "/static/images/desc1.png",
        price: 'asc',
        selectAll: 0,
        selectBySoldOut: 0,
        selectByNew: 0,
        selectByPrice: 1,
        leixingShow: false,
        blankBoxShow: false,
        sort: 'priceAsc'
      })
    } else {
      this.setData({
        imageurl2: "/static/images/desc-down.png",
        imageurl3: "/static/images/desc1.png",
        imageurl1: "/static/images/desc1.png",
        price: 'desc',
        selectAll: 0,
        selectBySoldOut: 0,
        selectByNew: 0,
        selectByPrice: 1,
        leixingShow: false,
        blankBoxShow: false,
        sort: 'priceDesc'
      })
    }
    this.clearData();
    this.loadList();
  },
  //最新
  choosesort3: function (e) {
    this.setData({
      imageurl3: "/static/images/desc1.png",
      imageurl1: "/static/images/desc1.png",
      imageurl2: "/static/images/desc.png",
      soldOut: 'asc',
      selectAll: 0,
      selectBySoldOut: 0,
      selectByPrice: 0,
      selectByNew: 1,
      leixingShow: false,
      blankBoxShow: false,
      sort: 'timeDesc'
    })
    this.clearData();
    this.loadList();
  }
})